import { _decorator, Component, Label } from 'cc';
const { ccclass, property } = _decorator;

import { ShuZiHuaRongDao_GameData } from "./ShuZiHuaRongDao_GameData";
import { ShuZiHuaRongDao_GameStorage } from "./ShuZiHuaRongDao_GameStorage";

@ccclass('ShuZiHuaRongDaoTimer')
export class ShuZiHuaRongDao_Timer extends Component {
      @property(Label)
      timeLabel: Label | null = null;
      public elapsedTime: number = 0;
      public running: boolean = false;
      private timer: number = 0;
      startTimer() {
            this.elapsedTime = ShuZiHuaRongDao_GameStorage.data[ShuZiHuaRongDao_GameData.Game_mode].current_data.time;

            this.running = true;
            this.updateTimeLabel();
      }
      stopTimer() {
            this.running = false;
      }
      resetTimer() {
            this.elapsedTime = 0;
            this.updateTimeLabel();
      }
      update(dt: number) {
            if (!this.running) return;
            this.elapsedTime += dt;

            this.timer += dt;
            if (this.timer >= 1) {
                  this.timer = 0;
                  this.updateTimeLabel();
            }
      }
      updateTimeLabel() {
            if (!this.timeLabel) return;
            const totalSeconds = Math.floor(this.elapsedTime);
            const minutes = Math.floor(totalSeconds / 60);
            const seconds = totalSeconds % 60;
            this.timeLabel.string = `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
            console.log(`updateTimeLabel`)

            ShuZiHuaRongDao_GameStorage.data[ShuZiHuaRongDao_GameData.Game_mode].current_data.time = this.elapsedTime;
            ShuZiHuaRongDao_GameStorage.saveData();
      }
}